<?php
namespace Admin\Controller;

class AdviceController extends AdminController {
    
    function _initialize() {
        parent::_initialize();
        $this->assign('sys', 'trade'); // 数据集
        $this->assign('action','advice');
    }

    //提问展示
    public function index(){
        if(IS_POST){
            $g = I('post.');
            if($g['start_time']){
                $g['start_time'] = strtotime($g['start_time']);
            }
            if($g['end_time']){
                $g['end_time'] = strtotime($g['end_time']);
            }

            if(trim($g['user_name'],"")){
                $where['user_name'] = array('like','%'.trim($g['user_name'],"").'%');
            }
            if($g['start_time'] && $g['end_time']){
                $where['addtime'] = array(array('gt',$g['start_time']),array('lt',$g['end_time']));
            }elseif($g['start_time']){
                $where['addtime'] = array(array('gt',$g['start_time']));
            }elseif($g['end_time']){
                $where['addtime'] = array(array('lt',$g['end_time']));
            }
        }else{
            $where = null;
        }

        $advice = M('advice');
        $count      = $advice->where($where)->order('addtime desc')->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $advice->where($where)->order('addtime desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出

        $this->display();
    }

    //回复提问
    public function answer(){
        if(IS_POST){
            $data['reply'] = trim(I('post.reply'),"");
            $where['id'] = I('post.id');
            $data['gtime'] = time();
            $re = M('advice')->where($where)->save($data);
            if($re){
                $this->success('答复完成',U('index'));
            }else{
                $this->error('失败请稍后重试');
            }
            die;
        }
        //提问的详细信息
        $where['id'] = I('get.id');
        $advice = M('advice')->where($where)->find();//用户提问详细信息
        $where2['user_id'] = $advice['user_id'];
        $advice['num'] = M('advice')->where($where2)->count();
        $this->assign('advice',$advice);
        $this->display();
    }

    //添加假提问
    public function addAdvice(){
        if(IS_POST){

            $p = I('post.');
            if(!$p['addtime']){
                $this->error('请填写提问时间');
            }

            if(!$p['gtime']){
                $this->error('请填写提问回复时间');
            }

            if(!$p['goods_id']){
                $this->error('请填写提问商品的id');
            }

            if(!$p['question']){
                $this->error('请填写用户提问的内容');
            }

            $p['addtime'] = strtotime($p['addtime'])+rand(0,1800);
            $p['gtime'] = strtotime($p['gtime'])+rand(1801,3600);

            if($p['addtime'] > $p['gtime']){
                $this->error('回复时间必须大于提问时间');
            }

            $p['goods_id'] = trim($p['goods_id']);
            $g = M('goods')->where(array('goods_id'=>$p['goods_id']))->find();
            if(!$g){
                $this->error('该商品id无效');
            }



            $data['addtime'] = $p['addtime'];
            $data['gtime'] = $p['gtime'];
            $data['goods_id'] = $p['goods_id'];
            $data['goods_name'] = $g['goods_title'];
            $data['user_id'] = 0;

            //随机用户名
            $file = fopen("Public/m.txt","r");
            $user=array();
            $i=0;
            while(!feof($file))
            {
                $user[$i]= fgets($file);
                $i++;
            }
            fclose($file);
            $user=array_filter($user);
            $sjnum = rand(0,$i);
            $data['user_name'] = $user[$sjnum];

            $data['question'] = trim($p['question']);

            if($p['reply']){
                $data['reply'] = trim($p['reply']);
            }

            $data['is_show'] = 1;

            $re = M('advice')->add($data);

            if($re){
                $this->success('新增成功', U('index'));
            }else{
                $this->error('添加失败请稍后重试');
            }
        }
        $this->display();
    }


    //ajax修改状态
    public function is_show(){
        $p = I('post.');
        $where['id'] = $p['id'];
        $data['is_show'] = $p['is_show'];
        $re = M('advice')->where($where)->save($data);
        $a['a'] = $re;
        $this->ajaxreturn($a);
    }

    //ajax删除记录
    public function del(){
        $p = I('post.');
        $where['id'] = $p['id'];
        $re = M('advice')->where($where)->delete();
        $a['a'] = $re;
        $this->ajaxreturn($a);
    }

}